package com.trex.modules.tbl.replyform.web;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.trex.modules.tbl.lineform.entity.TblXdZxxdctzd;
import com.trex.modules.tbl.lineform.service.TblXdZxxdctzdService;
import com.trex.modules.tbl.workerplace.dao.TblZdWorkerdzDao;
import com.trex.modules.utils.JsonGenerator;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.trex.modules.tbl.checkman.entity.TblZdJcr;
import com.trex.modules.tbl.checkman.service.TblZdJcrService;
import com.trex.modules.tbl.dw.service.TblDwProService;
import com.trex.modules.tbl.entity.TblXdLjzyjl;
import com.trex.modules.tbl.kclb.entity.TblZdKclb;
import com.trex.modules.tbl.kclb.entity.TblZdKcyy;
import com.trex.modules.tbl.kclb.service.TblZdKclbService;
import com.trex.modules.tbl.kclb.service.TblZdKcyyService;
import com.trex.modules.tbl.place.entity.TblZdZycs;
import com.trex.modules.tbl.place.service.TblZdZycsService;
import com.trex.modules.tbl.replyform.entity.TblXdJxchsd;
import com.trex.modules.tbl.replyform.service.TblXdJxchsdService;
import com.trex.modules.tbl.service.TblXdLjzyjlService;
import com.trex.modules.utils.TimeUtils;

import static com.trex.modules.utils.JsonGeneratorDetail.createJson;
import static com.trex.modules.utils.JsonSenderUtil.sendJsonWithHttpClient;

/**
 * 检修车回送单Controller
 * @author jy
 * @version 2022-12-09
 */
@Controller
@RequestMapping(value = "/tbl/replyform/tblXdJxchsd")
public class TblXdJxchsdController extends BaseController {

	@Autowired
	private TblXdJxchsdService tblXdJxchsdService;

	@Autowired
	private TblZdJcrService tblZdJcrService;

	@Autowired
	private TblZdKclbService tblZdKclbService;

	@Autowired
	private TblZdKcyyService tblZdKcyyService;

	@Autowired
	private TblXdLjzyjlService tblXdLjzyjlService;

	@Autowired
	private TblZdZycsService tblZdZycsService;

	@Autowired
	private TblDwProService tblDwProService;

	@Resource
	private AmqpTemplate amqpTemplate;

	@Autowired
	private TblXdZxxdctzdService tblXdZxxdctzdService;



	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TblXdJxchsd get(String recId, boolean isNewRecord) {
		return tblXdJxchsdService.get(recId, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = {"list", ""})
	public String list(@RequestParam(value = "userid", required = false) String userid,
					   @RequestParam(value = "postid", required = false) String postid,TblXdJxchsd tblXdJxchsd, Model model,HttpServletRequest request) {
		LocalDateTime now = LocalDateTime.now();
		logger.info("Received userid:\"!!!!!!!!!!!!!!!!!!!!!!!!!\"+\"______\"+ " + userid);


		HttpSession session = request.getSession();
		String sessionId = session.getId();
		if (userid != null){
			tblXdLjzyjlService.insertUser(userid,sessionId,now);
		}

//		String userId = tblXdLjzyjlService.selectUserData(sessionId);
		String userId = JsonGenerator.extractAndDecodeAccessToken(request);
		logger.info("9999999999999999999999+_____" + userId);
		model.addAttribute("tblXdJxchsd", tblXdJxchsd);

		List<TblXdZxxdctzd> lineList = tblXdZxxdctzdService.selectLinereply(userId); ;
		model.addAttribute("lineList", lineList);

		List<TblXdZxxdctzd> lineList2 = new ArrayList<>();
		model.addAttribute("lineListByStation", lineList2);
		return "modules/tbl/replyform/tblXdJxchsdList";
	}


	/**
	 * 查询列表
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = {"list1", ""})
	public String list1(@RequestParam(value = "userid", required = false) String userid,
					   @RequestParam(value = "postid", required = false) String postid,TblXdJxchsd tblXdJxchsd, Model model,HttpServletRequest request) {
		LocalDateTime now = LocalDateTime.now();
		logger.info("Received userid:\"!!!!!!!!!!!!!!!!!!!!!!!!!\"+\"______\"+ " + userid);

		String I_IP_ADDR = request.getRemoteHost();
		model.addAttribute("tblXdJxchsd", tblXdJxchsd);
		List<TblXdZxxdctzd> lineList = tblXdZxxdctzdService.selectLine1(userid);
		model.addAttribute("lineList", lineList);

		List<TblXdZxxdctzd> lineList2 = new ArrayList<>();
		model.addAttribute("lineListByStation", lineList2);
		return "modules/tbl/replyform/tblXdJxchsdList1";
	}


	/**
	 * 查询列表数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "listData1")
	@ResponseBody
	public Page<TblXdJxchsd> listData1(TblXdJxchsd tblXdJxchsd, HttpServletRequest request, HttpServletResponse response) throws ParseException {
		tblXdJxchsd.setPage(new Page<>(request, response));
		String userId = JsonGenerator.extractAndDecodeAccessToken(request);
		String strWhere = "";

		// 检查车号是否存在且不为空
		if (tblXdJxchsd.getWagonno() != null && !tblXdJxchsd.getWagonno().isEmpty()) {
			String xlbh = tblXdJxchsd.getWagonno();
			strWhere += " AND A.WAGONNO = '" + xlbh + "' ";
		}

		// 检查时间范围是否存在
		if (tblXdJxchsd.getStartTime() != null && tblXdJxchsd.getEndTime() != null) {
			String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tblXdJxchsd.getStartTime());
			String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tblXdJxchsd.getEndTime());
			strWhere += " AND A.STATION_TIME >= '" + startTime + "' "
					+ " AND A.STATION_TIME <= '" + endTime + "' ";
		}

		// 如果车号或时间至少有一个条件存在，执行查询
		if (!strWhere.isEmpty()) {
			strWhere +="AND A.FAULT_CODE IS NOT NULL";
			tblXdJxchsd.getSqlMap().put("extWhere", strWhere);
			tblXdJxchsd.setUserid(userId);
			Page<TblXdJxchsd> page = tblXdJxchsdService.findPage(tblXdJxchsd);
			return page;
		}

		// 如果没有输入条件，返回null
		return null;
	}


	/**
	 * 查询列表数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TblXdJxchsd> listData(TblXdJxchsd tblXdJxchsd, HttpServletRequest request, HttpServletResponse response) {
		if (tblXdJxchsd.getDqxlbh() != null || tblXdJxchsd.getRecId() != null && !tblXdJxchsd.getRecId().isEmpty()){
			tblXdJxchsd.setPage(new Page<>(request, response));
//			HttpSession session = request.getSession();
//			String sessionId = session.getId();
//			String userId = tblXdLjzyjlService.selectUserData(sessionId);
			String userId = JsonGenerator.extractAndDecodeAccessToken(request);
			logger.info("9999999999999999999999+_____" + userId);
			tblXdJxchsd.setUserid(userId);

			String strWhere = "";
			String xlbh = tblXdJxchsd.getDqxlbh();
			if (xlbh != null) {
				strWhere += " AND B.STATUS = '1' AND B.LINE_CODE = '" + xlbh + "' ";
				tblXdJxchsd.getSqlMap().put("extWhere", strWhere);
			}
			Page<TblXdJxchsd> page = tblXdJxchsdService.findPage(tblXdJxchsd);
		return page;
	}
		return null;
	}



	@RequestMapping(value = "listData5")
	@ResponseBody
	public Page<TblXdJxchsd> listData5(TblXdJxchsd tblXdJxchsd, HttpServletRequest request, HttpServletResponse response) {
		if (tblXdJxchsd.getDqxlbh() != null || tblXdJxchsd.getRecId() != null && !tblXdJxchsd.getRecId().isEmpty()){
			tblXdJxchsd.setPage(new Page<>(request, response));
			String userId = JsonGenerator.extractAndDecodeAccessToken(request);
			logger.info("9999999999999999999999+_____" + userId);
			tblXdJxchsd.setUserid(userId);

			String strWhere = "";
				strWhere += " AND  A.LJZYBH  = '" + tblXdJxchsd.getRecId() + "' ";
				tblXdJxchsd.setRecId(null);
				tblXdJxchsd.getSqlMap().put("extWhere", strWhere);
			Page<TblXdJxchsd> page = tblXdJxchsdService.findPage(tblXdJxchsd);
			return page;
		}
		return null;
	}


	/**
	 * 查询列表数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "listInfo")
	@ResponseBody
	public Page<TblXdJxchsd> listInfo(TblXdJxchsd tblXdJxchsd, HttpServletRequest request, HttpServletResponse response) {
		if (tblXdJxchsd.getDqxlbh() != null || tblXdJxchsd.getRecId() != null && !tblXdJxchsd.getRecId().isEmpty()){
			tblXdJxchsd.setPage(new Page<>(request, response));
			String userId = JsonGenerator.extractAndDecodeAccessToken(request);
			tblXdJxchsd.setUserid(userId);
			tblXdJxchsd.setLjzybh(tblXdJxchsd.getRecId());
			String strWhere = "A.LJZYBH = #{ljzybh} ";
			tblXdJxchsd.getSqlMap().put("extWhere", strWhere);

			String xlbh = tblXdJxchsd.getDqxlbh();
			if (xlbh != null) {
				strWhere += " AND B.STATUS = '1' AND B.LINE_CODE = '" + xlbh + "' ";
				tblXdJxchsd.getSqlMap().put("extWhere", strWhere);
			}
			tblXdJxchsd.setRecId(null);
			Page<TblXdJxchsd> page = tblXdJxchsdService.findPage(tblXdJxchsd);
			return page;
		}
		return null;
	}

	/**
	 * 查看编辑表单
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "form")
	public String form(TblXdJxchsd tblXdJxchsd, Model model) {
		List<TblZdJcr> jcrList = tblZdJcrService.findList(new TblZdJcr()); // 检车人
		List<TblZdKclb> kclbList = tblZdKclbService.findList(new TblZdKclb()); //扣车类别
		List<TblZdKcyy> kcyyList = tblZdKcyyService.findList(new TblZdKcyy()); //扣车原因

//		List<Map<String,String>> sendStationList = tblZdJcrService.findStation();
//		List<Map<String,String> >arriveStationList = tblZdJcrService.findStation();
//		List<Map<String,String>> repairLineList = tblZdJcrService.findStation();
		List<TblXdZxxdctzd> rlcList = tblZdKcyyService.findrepairLine(new TblXdZxxdctzd());
		model.addAttribute("rlcList", rlcList);
		model.addAttribute("kclbList", kclbList);
		model.addAttribute("jcrList", jcrList);
		model.addAttribute("kcyyList", kcyyList);
		model.addAttribute("tblXdJxchsd", tblXdJxchsd);
//		model.addAttribute("sendStationList" ,sendStationList);
//		model.addAttribute("arriveStationList" ,arriveStationList);
//		model.addAttribute("repairLineList", repairLineList);
		model.addAttribute("ids", tblXdJxchsd.getIds());

		return "modules/tbl/replyform/tblXdJxchsdForm";
	}



	/**
	 * 查看编辑表单
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "form1")
	public String form1(TblXdJxchsd tblXdJxchsd, Model model) {
		List<TblZdJcr> jcrList = tblZdJcrService.findList(new TblZdJcr()); // 检车人
		List<TblZdKclb> kclbList = tblZdKclbService.findList(new TblZdKclb()); //扣车类别
		List<TblZdKcyy> kcyyList = tblZdKcyyService.findList(new TblZdKcyy()); //扣车原因
//		List<Map<String,String>> sendStationList = tblZdJcrService.findStation();
//		List<Map<String,String> >arriveStationList = tblZdJcrService.findStation();
//		List<Map<String,String>> repairLineList = tblZdJcrService.findStation();
		List<TblXdZxxdctzd> rlcList = tblZdKcyyService.findrepairLine(new TblXdZxxdctzd());
		model.addAttribute("rlcList", rlcList);
		model.addAttribute("kclbList", kclbList);
		model.addAttribute("jcrList", jcrList);
		model.addAttribute("kcyyList", kcyyList);
		model.addAttribute("tblXdJxchsd", tblXdJxchsd);
//		model.addAttribute("sendStationList" ,sendStationList);
//		model.addAttribute("arriveStationList" ,arriveStationList);
//		model.addAttribute("repairLineList", repairLineList);
		model.addAttribute("ids", tblXdJxchsd.getIds());

		return "modules/tbl/replyform/tblXdJxchsdForm1";
	}

	/**
	 * 保存数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:edit")
	@PostMapping(value = "save")
	@ResponseBody
	@JsonIgnore
	public String save(@Validated TblXdJxchsd tblXdJxchsd, HttpServletRequest request) throws Exception {
		try {
			tblXdJxchsdService.saveInfo(tblXdJxchsd, request);
			return renderResult(Global.TRUE, text("修改成功"));
		} catch (Exception e) {
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
	}

	/**
	 * 保存数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:edit")
	@PostMapping(value = "save1")
	@ResponseBody
	@JsonIgnore
	public String save1(@Validated TblXdJxchsd tblXdJxchsd, HttpServletRequest request) throws Exception {
		try {

			tblXdJxchsdService.sendInfo1(tblXdJxchsd, request);
			return renderResult(Global.TRUE, text("成功"));
		} catch (Exception e) {
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
	}




	/**
	 * 删除数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(HttpServletRequest request,String jhhString) throws Exception {

		try{
			tblXdJxchsdService.deleteInfo(jhhString,request);
			return renderResult(Global.TRUE, text("检修车回送单删除成功！"));
		} catch (Exception e) {
			return renderResult(Global.FALSE, text(e.getMessage()));
		}


	}


	/**
	 * 删除数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:edit")
	@RequestMapping(value = "send")
	@ResponseBody
	public String send(HttpServletRequest request,String jhhString) throws Exception {

//		try{
			System.out.println(jhhString);
			tblXdJxchsdService.sendInfo(jhhString,request);
			return renderResult(Global.TRUE, text("检修车回送单偏重发送成功！"));
//		} catch (Exception e) {
//			return renderResult(Global.FALSE, text(e.getMessage()));
//		}


	}

	/**
	 * 删除数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:edit")
	@RequestMapping(value = "back")
	@ResponseBody
	public String back(HttpServletRequest request,String jhhString) throws Exception {
//		try{
			System.out.println(jhhString);
//			tblXdJxchsdService.deleteInfo(jhhString,request);
			tblXdJxchsdService.backInfo(jhhString,request);
			return renderResult(Global.TRUE, text("检修车回送单撤回偏重成功！"));
//		} catch (Exception e) {
//			return renderResult(Global.FALSE, text(e.getMessage()));
//		}


	}
	
	/**
	 * 线路字典数据查询
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = {"getLineList", ""})
	public List<TblXdLjzyjl> getLineList(String stationCode){
		List<TblXdLjzyjl> lineListByStation = tblXdLjzyjlService.getLineListByStation(stationCode, UserUtils.getUser().getUserCode());
		return lineListByStation;
	}
	@RequestMapping(value = {"getLineList1", ""})
	public List<TblXdLjzyjl> getLineList1(String stationCode){
		List<TblXdLjzyjl> lineListByStation = tblXdLjzyjlService.getLineListByStation1(stationCode, UserUtils.getUser().getUserCode());
		return lineListByStation;
	}





	/**
	 * 检修车回送单查询画面
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = {"list2", ""})
	public String list2(TblXdJxchsd tblXdJxchsd, Model model) {
		int hours = new Date().getHours();
		if(hours >= 8 &&  hours < 19) {
			tblXdJxchsd.setStation("2");
		}else {
			tblXdJxchsd.setStation("1");
		}
		tblXdJxchsd.setWorkStartTime(TimeUtils.getCurrentDate());
		
		String userCode = UserUtils.getUser().getUserCode(); // 取用户id

		List<TblZdZycs> tblZdZycsList = tblZdZycsService.findLJDeptList(userCode);
		List<TblZdZycs> tblZdZycsList2 = tblZdZycsService.findLJDeptListByBH("");
		model.addAttribute("tblZdZycsList", tblZdZycsList);
		model.addAttribute("tblZdZycsList2", tblZdZycsList2);
		model.addAttribute("tblXdJxchsd", tblXdJxchsd);
		return "modules/tbl/replyform/tblXdJxchsdList2";
	}
	
	/**
	 * 检修车回送单查询画面列表数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "listData2")
	@ResponseBody
	public Page<TblXdJxchsd> listData2(TblXdJxchsd tblXdJxchsd, HttpServletRequest request, HttpServletResponse response) {
		String userId = JsonGenerator.extractAndDecodeAccessToken(request);
		tblXdJxchsd.setPage(new Page<>(request, response));
		tblXdJxchsd.setUserid(userId);
		String strWhere = "";
		String xlbh =  tblXdJxchsd.getDqxlbh();
		if(xlbh !=null){
			strWhere += "  AND B.LINE_CODE = '"+xlbh+"' ";
			tblXdJxchsd.getSqlMap().put("extWhere", strWhere);
		}
		Page<TblXdJxchsd> page = tblXdJxchsdService.findPage(tblXdJxchsd);
		return page;
	}
	
	/**
	 * 加载列检所数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "findCsbhList")
	@ResponseBody
	public List<TblZdZycs> findCsbhList(String deptCode, HttpServletRequest request, HttpServletResponse response) {
		return tblZdZycsService.findLJDeptListByBH(deptCode);
	}
	
	/**
	 * 列检调度确认小票画面
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = {"list3", ""})
	public String list3(@RequestParam(value = "userid", required = false) String userid,
						@RequestParam(value = "postid", required = false) String postid,TblXdJxchsd tblXdJxchsd, Model model , HttpServletRequest request) {
		LocalDateTime now = LocalDateTime.now();
		String I_IP_ADDR = request.getRemoteHost();
		HttpSession session = request.getSession();
		String sessionId = session.getId();
		if (userid != null){
			tblXdLjzyjlService.insertUser(userid,sessionId,now);
		}

//		String userId = tblXdLjzyjlService.selectUserData(sessionId);
		String userId = JsonGenerator.extractAndDecodeAccessToken(request);
		logger.info("9999999999999999999999+_____" + userId);
		tblXdJxchsd.setUserid(userId);
		int hours = new Date().getHours();
		if(hours >= 8 &&  hours < 19) {
			tblXdJxchsd.setStation("2");
		}else {
			tblXdJxchsd.setStation("1");
		}
		tblXdJxchsd.setWorkStartTime(TimeUtils.getCurrentDate());
		if (userid != null) {
			// 将 userid 添加到模型中或者在逻辑中使用
			tblXdJxchsd.setUserid(userid);
		}
		if (postid != null) {
			// 将 postid 添加到模型中或者在逻辑中使用
			tblXdJxchsd.setPostid(postid);
		}
		
		String userCode = UserUtils.getUser().getUserCode(); // 取用户id

		List<TblZdZycs> tblZdZycsList = tblZdZycsService.findLJDeptList(userCode);
		List<TblZdZycs> tblZdZycsList2 = tblZdZycsService.findLJDeptListByBH("");
		model.addAttribute("tblZdZycsList", tblZdZycsList);
		model.addAttribute("tblZdZycsList2", tblZdZycsList2);
		model.addAttribute("tblXdJxchsd", tblXdJxchsd);
		return "modules/tbl/replyform/tblXdJxchsdList3";
	}
	
	/**
	 * 列检调度确认小票列表数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "listData3")
	@ResponseBody
	public Page<TblXdJxchsd> listData3(TblXdJxchsd tblXdJxchsd, HttpServletRequest request, HttpServletResponse response) throws ParseException {
		tblXdJxchsd.setPage(new Page<>(request, response));
//		HttpSession session = request.getSession();
//		String sessionId = session.getId();
//		String userId = tblXdLjzyjlService.selectUserData(sessionId);
		String userId = JsonGenerator.extractAndDecodeAccessToken(request);
		logger.info("9999999999999999999999+_____" + userId);
		tblXdJxchsd.setUserid(userId);
		String strWhere = "";
		//如果车号模糊查询复选框已打勾，则忽略查询日期、班别、列检调度是否确认条件，全表搜车号符合条件的记录，不打勾正常查询
		if (StringUtils.isEmpty(tblXdJxchsd.getRemark())) {
			String shift = tblXdJxchsd.getStation(); //班制
			Date workStartTime = tblXdJxchsd.getStationTime(); //日期
			if (workStartTime != null) {
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				formatter.format(workStartTime);
				Calendar cal = new GregorianCalendar();
				cal.setTime(workStartTime);
				if ("1".equals(shift)) {
					//夜班
					cal.add(Calendar.DATE, -1);
					Date workStartTimeE = cal.getTime();
					String timeEnded = null;
					String timeGte = formatter.format(workStartTime);
					String timeLte = formatter.format(workStartTimeE);
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					timeEnded = timeLte.substring(0, 10) + " 19:00:00";
					timeGte = timeGte.substring(0, 10) + " 08:00:00";
					timeLte = timeEnded;
					tblXdJxchsd.setWorkStartTime_gte(format.parse(timeLte));
					tblXdJxchsd.setWorkStartTime_lte(format.parse(timeGte));
				} else {
					cal.add(Calendar.DATE, 1);
					Date workStartTimeE = cal.getTime();
					String timeGte = formatter.format(workStartTime);
					String timeLte = formatter.format(workStartTimeE);
					//白班
					timeGte = timeGte.substring(0, 10) + " 08:00:00";
					timeLte = timeGte.substring(0, 10) + " 19:00:00";
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					tblXdJxchsd.setWorkStartTime_gte(format.parse(timeGte));
					tblXdJxchsd.setWorkStartTime_lte(format.parse(timeLte));
				}

				tblXdJxchsd.setWorkStartTime(null);

				if ("1".equals(tblXdJxchsd.getStatus())) {
					//列检调度确认
					tblXdJxchsd.setStatus("2");
					strWhere = "  AND A.STATUS = '2' ";
				}
				if ("0".equals(tblXdJxchsd.getStatus())) {
					//列检调度未确认
					tblXdJxchsd.setStatus("1");
					strWhere = "  AND A.STATUS = '1' ";
				}
			tblXdJxchsd.setWagonno_like(tblXdJxchsd.getWagonno());
			strWhere += "  AND FAULT_ITEM_CODE IS NOT NULL ";
			tblXdJxchsd.getSqlMap().put("extWhere", strWhere);

			Page<TblXdJxchsd> page = tblXdJxchsdService.findPage(tblXdJxchsd);
			return page;
		}
	}
	return null;
	}
	
	/**
	 * 列检调度确认小票
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "confirmData")
	@ResponseBody
	public String confirmData(String ids, String type_LC, HttpServletRequest request) throws Exception {
		try {
			tblXdJxchsdService.confirmDataInfo(ids, type_LC, request);
			return renderResult(Global.TRUE, text("修改成功"));
		}catch (Exception e) {
			return renderResult(Global.FALSE, text(e.getMessage()));
		}

	}
	
	/**
	 * 列检调度取消确认小票
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "removeData")
	@ResponseBody
	public String removeData(String ids, String type_LC, HttpServletRequest request) throws Exception {
try{
	tblXdJxchsdService.removeDataInfo(ids, type_LC,request);
	return renderResult(Global.TRUE, text("撤销成功"));
}catch (Exception e) {
	return renderResult(Global.FALSE, text(e.getMessage()));
}

		}

	
	/**
	 * 列检调度确认小票画面
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = {"list4", ""})
	public String list4(@RequestParam(value = "userid", required = false) String userid,
						@RequestParam(value = "postid", required = false) String postid,TblXdJxchsd tblXdJxchsd, Model model) {
		int hours = new Date().getHours();
		if(hours >= 8 &&  hours < 19) {
			tblXdJxchsd.setStation("2");
		}else {
			tblXdJxchsd.setStation("1");
		}

		if (userid != null) {
			// 将 userid 添加到模型中或者在逻辑中使用
			tblXdJxchsd.setUserid(userid);
		}
		if (postid != null) {
			// 将 postid 添加到模型中或者在逻辑中使用
			tblXdJxchsd.setPostid(postid);
		}
		tblXdJxchsd.setWorkStartTime(TimeUtils.getCurrentDate());
		
		String userCode = UserUtils.getUser().getUserCode(); // 取用户id

		List<TblZdZycs> tblZdZycsList = tblZdZycsService.findLJDeptList(userCode);
		List<TblZdZycs> tblZdZycsList2 = tblZdZycsService.findLJDeptListByBH("");
		model.addAttribute("tblZdZycsList", tblZdZycsList);
		model.addAttribute("tblZdZycsList2", tblZdZycsList2);
		model.addAttribute("tblXdJxchsd", tblXdJxchsd);
		return "modules/tbl/replyform/tblXdJxchsdList4";
	}
	
	/**
	 * 列检调度确认小票列表数据
	 */
	//@RequiresPermissions("tbl:replyform:tblXdJxchsd:view")
	@RequestMapping(value = "listData4")
	@ResponseBody
	public Page<TblXdJxchsd> listData4(TblXdJxchsd tblXdJxchsd, HttpServletRequest request, HttpServletResponse response) {
		tblXdJxchsd.setPage(new Page<>(request, response));
		String strWhere = "";
		//如果车号模糊查询复选框已打勾，则忽略查询日期、班别、列检调度是否确认条件，全表搜车号符合条件的记录，不打勾正常查询
		if(StringUtils.isEmpty(tblXdJxchsd.getRemark())) {
			String shift = tblXdJxchsd.getStation(); //班制
			Date workStartTime = tblXdJxchsd.getWorkStartTime(); //日期
			if(workStartTime != null) {
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				formatter.format(workStartTime);
				Calendar cal = new GregorianCalendar();
				cal.setTime(workStartTime);
				cal.add(Calendar.DATE, 1);
				Date workStartTimeE = cal.getTime();
				String timeGte = formatter.format(workStartTime);
				String timeLte = formatter.format(workStartTimeE);
				
				if("1".equals(shift)){
					//夜班
					timeGte = timeGte.substring(0, 10)+" 19:00:00";
					timeLte = timeLte.substring(0, 10)+" 08:00:00";
				}else{
					//白班
					timeGte = timeGte.substring(0, 10)+" 08:00:00";
					timeLte = timeGte.substring(0, 10)+" 19:00:00";
				}
				
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				try {
					tblXdJxchsd.setWorkStartTime_gte(format.parse(timeGte));
					tblXdJxchsd.setWorkStartTime_lte(format.parse(timeLte));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				tblXdJxchsd.setWorkStartTime(null);
			}
		}else {
			tblXdJxchsd.setWorkStartTime_gte(null);
			tblXdJxchsd.setWorkStartTime_lte(null);
			tblXdJxchsd.setWorkStartTime(null);
		}
		//strWhere = " AND STATUS >=2 AND CSBH IN (SELECT CSBH FROM VZD_LJCDDZ WHERE WORKER_ID = '00254112')";
		strWhere = " AND STATUS >=2 ";
		tblXdJxchsd.setWagonno_like(tblXdJxchsd.getWagonno());
		
		tblXdJxchsd.getSqlMap().put("extWhere", strWhere);
		
		Page<TblXdJxchsd> page = tblXdJxchsdService.findPage(tblXdJxchsd);
		return page;
	}
	/*M2NE44*/

	@CrossOrigin(origins = {"http://172.16.16.129:5080", "http://172.16.16.200:5080"})   // 允许的请求来源
	@PostMapping("/receiveCar")
	public ResponseEntity<String> receiveCar(@RequestBody JSONObject json) throws Exception {
		// 打印接收到的 JSON 数据
		logger.info("Received JSON: !!!!!!!!!!!!!!!!!!!!!!!!!!!!" + json.toJSONString());
		tblXdZxxdctzdService.receiveCar(json);

		// 返回接收到的数据
		return ResponseEntity.ok("success");
	}

}